<?php

namespace app\admin\model;

use think\Model;

class LeescoreOrder extends Model
{
    // 表名
    protected $name = 'leescore_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $payTime = 'paytime';
    protected $updateTime = false;
    // 追加属性
    protected $append = [
        'status_text',
        'paytime_text',
        'createtime_text'
    ];

    public function user()
    {
        return $this->belongsTo('User', 'uid')->setEagerlyType(0);
    }

    public function getOrderGoods()
    {
        return $this->hasMany('leescoreOrderGoods', 'order_id');
    }

    public function addressInfo()
    {
        return $this->belongsTo('Leeaddress', 'address_id');
    }

    public function getStatusList()
    {
        return ['-2' => '驳回', '-1' => '取消订单', '0' => '未支付', '1' => '已支付', '2' => '已发货', '3' => '已签收', '4' => '退货中', '5' => '已退款'];

    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['paytime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : __('Status 0');
    }

    protected function setPaytimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : '$value';
    }

    public function getCreatetimeTextAttr($value, $data) {
        $value = $value ? $value : $data['createtime'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : '';
    }

    public function orderGoods()
    {
        return $this->hasMany('leescore_order_goods', 'order_id');
    }
}
